
function pluralize(time, label) {
  if (time === 1) {
    return time + label
  }
  return time + label + 's'
}

export function arrFindToObj(key,arr){   // 由键值查找在数组中对应的对象
  return arr.find(item => item[key] === key)
}

export function timeAgo(time) {
  const between = Date.now() / 1000 - Number(time)
  if (between < 3600) {
    return pluralize(~~(between / 60), ' minute')
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), ' hour')
  } else {
    return pluralize(~~(between / 86400), ' day')
  }
}
export function timeago(time){
  const d = new Date(+time)
  return (d.getFullYear() + '-' + (d.getMonth() +1 ).toString().padStart(2,'0') + '-' + (d.getDate() +1 ).toString().padStart(2,'0'))
}
export function formatSize(value) {
  if (null === value || value === "") {
    return "0B"
  }
  let unitArr = new Array("B", "KB", "MB", "GB", "TB")
  let index = 0
  let srcsize = parseFloat(value)
  index = Math.floor(Math.log(srcsize) / Math.log(1024))
  let size = srcsize / Math.pow(1024, index)
  if (index > 0) {
    size = size.toFixed(2)
  }
  return size + unitArr[index]
}

export function formatNumber(num, precision, separator) {
  let parts
  if (!isNaN(parseFloat(num)) && isFinite(num)) {
    // 把类似 .5, 5. 之类的数据转化成0.5, 5, 为数据精度处理做准备, 至于为什么
    // 不在判断中直接写 if (!isNaN(num = parseFloat(num)) && isFinite(num))
    // 是因为parseFloat有一个奇怪的精度问题, 比如 parseFloat(12312312.1234567119)
    // 的值变成了 12312312.123456713
    num = Number(num)
    // Process decimal digits
    num = (typeof precision !== "undefined" ? num.toFixed(precision) : num).toString()
    // separate the decimal part and integral part of a number
    parts = num.split(".")
    // add [separator] to integral part, use a known regx
    parts[0] = parts[0].toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1" + (separator || ","))
    return parts.join(".")
  }
  return NaN
}

export function id2name(id,arr){
  arr.map(res => {
    if(res.id === id){
      return res
    }
  })
}

export function uppercaseFirst(string) {
  return string.charAt(0).toUpperCase() + string.slice(1)
}

// timestamp
const padding = (s, len) => {
  const _len = len - (`${s}`).length
  for (let i = 0; i < _len; i++) {
    s = `0${s}`
  }
  return s
}
const formatDatetime = (date, pattern = 'yyyy-MM-dd') => {
  return pattern.replace(/([yMdhsm])(\1*)/g, ($0) => {
    switch ($0.charAt(0)) {
    case 'y': return padding(date.getFullYear(), $0.length)
    case 'M': return padding(date.getMonth() + 1, $0.length)
    case 'd': return padding(date.getDate(), $0.length)
    case 'w': return date.getDay() + 1
    case 'h': return padding(date.getHours(), $0.length)
    case 'm': return padding(date.getMinutes(), $0.length)
    case 's': return padding(date.getSeconds(), $0.length)
    }
  })
}
export const formatStamp = (timestamp) => {
  if (!timestamp) {
    return '--'
  }
  const date = new Date(timestamp)
  return formatDatetime(date, 'yyyy-MM-dd')
  // return formatDatetime(date, 'yyyy-MM-dd hh:mm:ss')
}
